/****************************************************************************

	COPYRIGHT(C) MAXSI SOFTWARE, JONAS 'SORTIE' TERMANSEN 2008, 2009, 2010

    This file is part of Maxsi Distribution.

    Maxsi Distribution is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Maxsi Distribution is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Maxsi Distribution. If not, see <http://www.gnu.org/licenses/>.

	*/ #include "MaxsiDistributionContributors.h" /*

	If you modify this file, please enter your name below and provide contact
	information in MaxsiDistributionContributors.h. For more information please see
	MaxsiDistributionContributors.h.
	
	Contributors to this file:

	- Jonas 'Sortie' Termansen
	- [your name here]

	MaxsiDistribution.dll
	Distributing since 2008

	Interface.cpp
	An interface to the Maxsi Distribution Platform.

****************************************************************************/

#include "MaxsiEngine.h"
#include "MaxsiDistributionLink.h"
#include "Data.h"
#include "Interface.h"
#include "Interfaces.h"
#include <io.h>
#include <direct.h>
#include <iostream> 
#include <fcntl.h>
#include <SYS\Stat.h>
	
	bool	MayLaunchProduct	=	true;

	extern "C"  __declspec( dllexport )	void*	__cdecl	GetInterface(char* Version)
	{
		if ( _stricmp(Version,"MaxsiDistributionInterface001") == 0 )
		{ return				GetMaxsiDistributionInterface(Version);	}
		if ( _stricmp(Version,"MaxsiAchievementsInterface001") == 0 )
		{ return				GetMaxsiAchievementsInterface(Version);	}
		//else if ( _stricmp(Version,"MaxsiDistributionInterface002") == 0 )
		//{
		//	// Add new interfaces here!
		//}

		// We couldn't provide the desired Interface.
		return NULL;
	}
	
	bool	Interface_DoDownload()
	{
		MayLaunchProduct	=	false;

		char*		ProductPIDSTR			=	UIToA((unsigned int)ProductPID);

		char*		EncProduct				=	EncodeURL(ProductName);
		char*		EncProductPIDSTR		=	EncodeURL(ProductPIDSTR);
		char*		EncMasterServer			=	EncodeURL(MasterServer);
		char*		EncInstallPath			=	EncodeURL(InstallPath);
		char*		EncUsername				=	EncodeURL(SteamUserName);
		char*		EncProductRestartCmd	=	EncodeURL(OnCompleted);

		char*		InstallerExePath		=	BuildString(4,
			MDRoot,
			MDDistribution,
			MDBinFolder,
			"MaxsiInstaller.exe"
			);

		str_replace(InstallerExePath,"/","\\");

		char*		UpdateCommand			=	BuildString(15, // 15
			"\"", // 1
			InstallerExePath, // 2
			"\" "
			"\"MaxsiDistribution://Update/?Product=", // 3
			EncProduct, // 4
			"&MasterServer=", // 5
			EncMasterServer, // 6
			"&InstallPath=", // 7
			EncInstallPath, // 8
			"&Username=", // 9
			EncUsername, // 10
			"&ProductRestartCmd=", // 11
			EncProductRestartCmd, // 12
			"&ProductPID=", // 13
			EncProductPIDSTR, // 14
			"\"" // 15
			);

		WinExec(UpdateCommand,SW_SHOW);

		delete[]	UpdateCommand;
		delete[]	InstallerExePath;
		delete[]	EncUsername;
		delete[]	EncInstallPath;
		delete[]	EncMasterServer;
		delete[]	EncProductPIDSTR;
		delete[]	EncProduct;

		delete[]	ProductPIDSTR;

		return true;
	}
	
	bool	Interface_NoInternet()
	{
		MayLaunchProduct	=	true;
		return true;
	}
	
	bool	Interface_NeedsUpdating()
	{
		// ?
		MayLaunchProduct	=	false;
		return true;
	}

	bool	Interface_ReportError(const char* Error)
	{
		bool	Result		=	false;
		char*	WorkingDir	=	GetWorkingDirectory();
		char*	File		=	BuildString(2,WorkingDir,"MaxsiDistributionError.txt");

		HANDLE	FileHandle	=	CreateFileA(File, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

		if (FileHandle == INVALID_HANDLE_VALUE)
		{
			delete[] WorkingDir;
			delete[] File;
			return false;
		}
		else
		{
			DWORD	Written;

			char* Header = "Maxsi Distribution: An error happened during this products's update. Please contact the developers for more information, or contact Maxsi Distribution at http://www.maxsi.dk/distribution/\r\n\r\n";
			WriteFile(FileHandle, Header, strlen(Header), &Written, NULL);
			WriteFile(FileHandle, Error, strlen(Error), &Written, NULL);
			
			SetEndOfFile(FileHandle);

			CloseHandle(FileHandle);

			char*		ReadMeCommand		=	BuildString(3,"notepad \"",File,"\"");

			WinExec(ReadMeCommand,SW_SHOW);

			delete[]	ReadMeCommand;
		}		

		delete[] WorkingDir;
		delete[] File;
		
		MayLaunchProduct	=	false;
		
		return Result;
	}

	bool	Interface_Info(const char* Error)
	{
		MayLaunchProduct	=	false;

		return true;
	}

	bool	Interface_NewsUpdated(const char* News)
	{
		return true;
	}

	bool	Interface_SetStatus(const char* Status)
	{
		return true;
	}

	bool	Interface_SetSubStatus(const char* Status)
	{
		return true;
	}

	bool	Interface_SetProgress(int Progress)
	{
		return true;
	}

	bool	Interface_DownloadNews(char* Host, char* Resource, char* Product, char* Format)
	{
		return true;
	}